<?php

namespace Home\Controller;

/**
 * 客栈数据交互接口
 * @modify trainyao 2015-12-31 创建
 */
class HotelController extends \Home\Controller\ParentController {

    protected function _initialize() {
        if (!in_array(ACTION_NAME, array('getRemindList'))) {
            parent::_initialize();
        }
        $this->validateUtil = new \Org\Util\Validate();
    }

    /**
     * 添加客栈
     * @modify trainyao 2015-12-31 创建
     * @modify wenhui 2017-5-12 添加时区
     * @modify wenhui 2018-02-05 添加货币 currencyId
     */
    public function addHotel() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,nameLength20',
            'city' => 'notnull,notempty,id',
            'district' => 'notnull,notempty,id',
            'universalTime' => 'notnull,notempty',
            'currencyId' => 'notnull,notempty,id',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $name = $param['hotel'];
        $cityId = $param['city'];
        $districtId = $param['district'];
        $universalTime = $param['universalTime'];
        $currencyId = $param['currencyId'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $addResult = $hotelLogic->addInfo($name, $cityId, $districtId, $universalTime, null, null, $currencyId);

        returnJson(200, 200, 'success');
    }

     /**
     * 添加义卖客栈
     * @modify wenhui 2017-9-18 创建
     */
    public function addCharityHotel() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,nameLength20',
            'city' => 'notnull,notempty,id',
            'district' => 'notnull,notempty,id',
            'universalTime' => 'notnull,notempty',
            'currencyId' => 'notnull,notempty,id',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $name = $param['hotel'];
        $cityId = $param['city'];
        $districtId = $param['district'];
        $universalTime = $param['universalTime'];
        $currencyId = $param['currencyId'];

        $hotelApplyLogic = A('Common/HotelApply', 'Logic');
        $hotelApplyLogic->addCharityHotel($name, $universalTime, $cityId, $districtId, $currencyId);

        returnJson(200, 200, 'success');
    }

    /**
     * 修改客栈信息
     * @modify trainyao 2016-01-04    创建
     */
    public function editHotel() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'name' => 'notempty,nameLength20',
            'city' => 'notempty,id',
            'district' => 'notempty,id',
            'universalTime' => 'notempty,notnull',
            'checkoutTime' => 'notnull,notempty,isHourMinute',
            'checkinTime' => 'notnull,notempty,isHourMinute',
            'splitTime' => 'notnull,notempty,isHourMinute',
        );

        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        if ($param['checkoutTime'] >= $param['checkinTime']) {
            returnJson(403, 419);
            return;
        }

        $hotelId = $param['hotel'];
        $hotelName = $param['name'];
        $cityId = $param['city'];
        $districtId = $param['district'];
        $universalTime = $param['universalTime'];
        $checkoutTime = $param['checkoutTime'];
        $checkinTime = $param['checkinTime'];
        $splitTime = $param['splitTime'];
        //检查客栈是否有效
        $hotelLogic = A('Common/Hotel', 'Logic');
        $hotelLogic->checkHotelActive($hotelId);

        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $hotelEntityModel = D('HotelEntity');
        // 开启事务
        $hotelEntityModel->startTrans();
        $commit = true;

        $editResult = $hotelEntityModel->saveInfo($hotelId, $hotelName, $cityId, $districtId, null, null, $universalTime);
        if (empty($editResult)) {
            $commit = false;
        }

        if ($commit) {
            $hotelInfoModel = D('HotelInfo');
            $saveResult = $hotelInfoModel->saveInfo($hotelId, $checkoutTime, $checkinTime, $splitTime);
            if ($saveResult == false) {
                $commit = false;
            }
        }

        if ($commit) {
            $hotelEntityModel->commit();
        } else {
            $hotelEntityModel->rollback();
            returnJson(500, 506, 'error');
        }

        

        $hotelDetailModel = D('HotelDetail');
        $hotelDetail = $hotelDetailModel->getInfoByHotel($hotelId);
        if(!empty($hotelDetail)){
            if($hotelDetail['name']!=$hotelName){
                //丢掉认证标志
                $where = array(
                    'hotelentity_id' => $hotelId
                );
                $data = array(
                    'name' => $hotelName,
                    'certified' => 0,
                );
                $hotelDetailModel->where($where)->save($data);
                //变为未申请
                $hotelAuthModel = D('HotelAuth');
                $data = array(
                    'status' => 0,
                );
                $hotelAuthModel->where($where)->save($data);
            }
        }

        returnJson(200, 200, 'success');
    }

    /**
     * 保存多个客栈禁用启用状态
     * @modify trainyao 2016-07-28    创建
	 * @modify zhangzhen 2016-12-27   修改掌柜添加为掌柜做子账号的权限问题
     */
    public function saveEnableStatus() {
        $param = I('post.');
        
        $saveEnableHotelIds = isset($param['enableHotel']) ? $param['enableHotel'] : array();
        $saveDisableHotelIds = isset($param['disableHotel']) ? $param['disableHotel'] : array();
        if (!empty($saveEnableHotelIds)) {
            $validatePass = $this->ajaxValidateReturn($this->validateUtil, implode(',', $saveEnableHotelIds), 'concatId');
            if (empty($validatePass)) {
                return;
            }
        }
        if (!empty($saveDisableHotelIds)) {
            $validatePass = $this->ajaxValidateReturn($this->validateUtil, implode(',', $saveDisableHotelIds), 'concatId');
            if (empty($validatePass)) {
                return;
            }
        }

        // 校验是否可以操作传入客栈
        $userId = getUserId();
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $hasRight = $adminAuthorityLogic->canSetStatus($userId, array_merge($saveEnableHotelIds, $saveDisableHotelIds));
		//之前的逻辑
		// if (empty($hasRight)) {
		// 	returnJson(403,202);
		// 	return;
		// }

		//去除不属于该账号的客栈

        if (!empty($hasRight)) {
			foreach($saveEnableHotelIds as $key1 => $oneSaveEnable){
				foreach($hasRight as $key2 => $oneHasRight){
					if($hasRight[$key2] == $saveEnableHotelIds[$key1]){
						unset($saveEnableHotelIds[$key1]);
					}
				}
			}
			foreach($saveDisableHotelIds as $key1 => $oneSaveEnable){
				foreach($hasRight as $key2 => $oneHasRight){
					if($hasRight[$key2] == $saveDisableHotelIds[$key1]){
						unset($saveDisableHotelIds[$key1]);
					}
				}
			}
        }
        // 保存客栈停用启用状态
        $hotelLogic = A('Common/Hotel', 'Logic');
        $hotelEntityModel = D('HotelEntity');
        $hotelEntityModel->startTrans();
        // 设置事务嵌套模式
        $this->setMultiTransMode(true);
        $commit = true;
        try {
            if (!empty($saveEnableHotelIds)) {
                $hotelLogic->saveHotelStatus($saveEnableHotelIds, 0);
            }
            if (!empty($saveDisableHotelIds)) {
                $hotelLogic->saveHotelStatus($saveDisableHotelIds, 1);
            }
        } catch (\Exception $e) {
            $commit = false;
            $hotelEntityModel->rollback();
            returnJson($e->getMessage());
            return;
        }

        $hotelEntityModel->commit();
        returnJson(200, 200);
    }

    /**
     * 第三方登录锁掌柜
     * @modify trainyao 2016-01-05    创建
     * @modify trainyao 2016-03-21 增加区号,邮箱
     */
    public function qrmLogin() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'password' => 'notnull,notempty,password',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (isset($param['areaCode'])) {
            $validateRule = array(
                'areaCode' => 'notempty,areaCode',
            );
            $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
            $areaCode = $param['areaCode'];
            $validateRule = array(
                'mobile' => 'notnull,notempty,globalMobile',
            );
            $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule, array(
                'globalMobile' => array('function', 'checkGlobalMobile,' . $areaCode, '403,130,error'),
            ));
            $mobile = $param['mobile'];
        } else {
            $validateRule = array(
                'email' => 'notempty,email',
            );
            $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
            $email = $param['email'];
        }

        $hotelId = $param['hotel'];
        //检查客栈是否有效
        $hotelLogic = A('Common/Hotel', 'Logic');
        $hotelLogic->checkHotelActive($hotelId);
        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        // 调用RPC接口登录,获取集群列表
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('MANAGE_QRCODE_URL'));
        $client->setEncryptMode(3);

        $data = array(
            'flag' => 'uclbrt',
            'username' => $areaCode ? $mobile : $email,
            'password' => md5($param['password']),
            'time' => time(),
        );
        if ($areaCode) {
            $data['areaCode'] = $areaCode;
        }

        $result = $client->loginForMeizhu($data);
        $result = json_decode($result, true);

        if (is_array($result)) {
            if ($result['httpstatus'] == 200) {
                session(C('QRM_AUTH_COMMUNITY_DATA'), $result['data']);
                returnJson(200, 200, $result['data']);
            } else if ($result['httpstatus'] == '403' && $result['status'] == '302') {
                returnJson(403, 407);
            }
        }
        returnJson(403, 403, 'error');
    }

    /**
     * 客栈绑定锁掌柜集群
     * @modify trainyao 2016-01-05    创建
     */
    public function bindCommunity() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'community' => 'notnull,notempty,communityNumber',
        );

        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        // 判断是否在session内有对应数据
        $sessionCommunityData = session(C('QRM_AUTH_COMMUNITY_DATA'));
        $sessionCommunityData = getArrayValueAsIndex($sessionCommunityData, 'no');
        $communityNo = $param['community'];
        if (empty($sessionCommunityData) || empty($sessionCommunityData[$communityNo])) {
            returnJson(403, 202, 'error');
        }

        $hotelId = $param['hotel'];
        //检查客栈是否有效
        $hotelLogic = A('Common/Hotel', 'Logic');
        $hotelLogic->checkHotelActive($hotelId);
        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $hotelEntityModel = D('HotelEntity');
        $hotelInfo = $hotelEntityModel->getInfo($hotelId);

        if (empty($hotelInfo)) {
            returnJson(403, 400, 'error');
        }
        if ($hotelInfo['status'] == IS_DELETE) {
            returnJson(403, 401, 'error');
        }

        $saveCommunityResult = $hotelEntityModel->saveInfo($hotelId, null, null, null, $communityNo);
        if (empty($saveCommunityResult)) {
            returnJson(500, 506, 'error');
        }

        // 查询锁掌柜的房间供前端对比
        $roomLogic = A('Common/Room', 'Logic');
        $qrmRoomData = $roomLogic->getQrmRoom($hotelId);
        $qrmRoomNames = array();
        foreach ($qrmRoomData as $qrmRoom) {
            $qrmRoomNames[] = $qrmRoom['no'];
        }
        returnJson(200, 200, $qrmRoomNames);
    }

    /**
     * 添加分销渠道
     * @modify trainyao 2016-01-08 创建
     */
    public function addChannel() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'name' => 'notnull,notempty,nameLength15',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $channelName = $param['name'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $addResult = $hotelLogic->addChannel($hotelId, $channelName);

        returnJson(200, 200);
    }

    /**
     * 删除分销渠道
     * @modify trainyao 2016-01-08 创建
     */
    public function deleteChannel() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'channel' => 'notnull,notempty,id',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $hotelChannelId = $param['channel'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $addResult = $hotelLogic->deleteChannel($hotelId, $hotelChannelId);

        returnJson(200, 200);
    }

    /**
     * 添加支付方式
     * @modify trainyao 2016-01-08 创建
     */
    public function addPay() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'name' => 'notnull,notempty,nameLength15',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $payName = $param['name'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $addResult = $hotelLogic->addPay($hotelId, $payName);

        returnJson(200, 200);
    }

    /**
     * 删除支付方式
     * @modify trainyao 2016-01-08 创建
     */
    public function deletePay() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'pay' => 'notnull,notempty,id',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $hotelPayId = $param['pay'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $addResult = $hotelLogic->deletePay($hotelId, $hotelPayId);

        returnJson(200, 200);
    }

    /**
     * 添加支付短信通知
     * @modify AmosZhong 2016-05-16 创建
     * @modify wenhui 2017-05-04 增加国际手机检验
     */
    public function addPayMobile() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            //'mobile' => 'notnull,notempty,mobile',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        $areaCode = I('post.areaCode', '86', 'string');
        $mobile = $param['mobile'];
        $this->ajaxValidateReturn($this->validateUtil, $mobile, 'globalMobile',
                    array('globalMobile' => array('function', 'checkGlobalMobile,' . $areaCode, '403,130,error'),
                ));

        $hotelId = $param['hotel'];
        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        $hotelPayLogic = A('Common/HotelPay', 'Logic');
        $hotelPayLogic->savePayMobile($hotelId, $mobile, $areaCode);

        returnJson(200, 200);
    }

    /**
     * 删除支付短信通知号码
     * @modify AmosZhong 2016-05-16 创建
     */
    public function deletePayMobile() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id'
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        $hotelPayLogic = A('Common/HotelPay', 'Logic');
        $hotelPayLogic->savePayMobile($hotelId, '');

        returnJson(200, 200);
    }

    /**
     * 保存结算方式信息
     * @modify AmosZhong 2016-05-16 创建
     * @modify wenhui 2017-05-04 增加国际手机检验
     */
    public function savePayDetail() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'mobile' => 'notnull,notempty',
            'type' => 'notnull,notempty,booleanInt',
            'bank' => 'notnull,notempty,nameLength20',
            'name' => 'notnull,notempty,nameLength20',
            'account' => 'notnull,notempty,bankNumber',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        $areaCode = I('post.areaCode', '86', 'string');
        $mobile = $param['mobile'];
        $this->ajaxValidateReturn($this->validateUtil, $mobile, 'globalMobile',
                    array('globalMobile' => array('function', 'checkGlobalMobile,' . $areaCode, '403,130,error'),
                ));

        $hotelId = $param['hotel'];
        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        $type = $param['type'];
        $bank = $param['bank'];
        $name = $param['name'];
        $account = $param['account'];
        $hotelPayLogic = A('Common/HotelPay', 'Logic');
        $hotelPayLogic->savePayDetail($hotelId, $mobile, $type, $bank, $name, $account, $areaCode);

        returnJson(200, 200);
    }

    /**
     * 保存退订规则
     * @modify AmosZhong 2016-05-16 创建
     * @modify trainyao 2016-06-20 添加押金信息
     */
    public function saveRefundRule() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'rule' => 'notnull,notempty,nameLength255',
            'deposit' => 'notnull,notempty,decimal8_2',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        $refundRule = $param['rule'];
        $deposit = $param['deposit'];
        $hotelPayLogic = A('Common/HotelPay', 'Logic');
        $hotelPayLogic->savePayRefundRule($hotelId, $refundRule, $deposit);

        returnJson(200, 200);
    }

    /**
     * 获取酒店支付设置详细信息
     * @modify AmosZhong 2016-05-16 创建
     */
    public function getPayDetail() {

        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id'
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        $hotelPayLogic = A('Common/HotelPay', 'Logic');
        $result = $hotelPayLogic->getPayDetail($hotelId);

        // 获取酒店预定模式 sort($roomTypeItem['priceList'], SORT_NUMERIC);
        $hotelBookDetail = $hotelBookLogic->getInfo($hotelId, true);
        // 获取客栈所有房型
        $roomTypLogic = A('Common/RoomType', 'Logic');
        $roomTypInfo = $roomTypLogic->getInfoByHotel($hotelId);
        $minusTypes = array_column($roomTypInfo, 'minustype');
        rsort($minusTypes, SORT_NUMERIC);
        $minusenable = $minusTypes[0] == 0 ? 0 : $hotelBookDetail['minusenable'];

        returnJson(200, 200, array(
            'refundrule' => $result['refundrule'],
            'deposit' => $result['deposit'],
            'areacode' => $result['areacode'],
            'orderpayareacode' => $result['payareacode'],
            'orderpaymobile' => $result['orderpaymobile'],
            'accountareacode' => $result['accountareacode'],
            'accountmobile' => $result['accountmobile'],
            'accounttype' => $result['accounttype'],
            'bankname' => $result['bankname'],
            'accountname' => $result['accountname'],
            'accountnumber' => $result['accountnumber'],
            'bookMode' => $hotelBookDetail['bookmode'],
            'minusEnable' => $minusenable,
        ));
    }

    /**
     * 获取客栈退款信息
     * @modify trainyao 2016-05-17    创建
     * @modify trainyao 2016-06-20 添加押金信息
     */
    public function refundItem() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'startDate' => 'notempty,isDate',
            'endDate' => 'notempty,isDate',
            'mobile' => 'notempty',
            'status' => 'notempty,refundStatus',
            'currentPage' => 'notempty,natureNumber',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        // 校验客栈是否过期,是否禁用
        $hotelId = $param['hotel'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $result = $hotelLogic->checkHotelActive($hotelId);
        if ($result == false) {
            returnJson(403, 408);
            return;
        }
        $result = $hotelLogic->checkHotelStatus($hotelId);
        if ($result == false) {
            returnJson(403, 401);
            return;
        }

        // 校验是否有操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $hasRight = $adminAuthorityLogic->check($hotelId);
        if (!$hasRight) {
            returnJson(403, 202);
            return;
        }

        // 获取信息
        $status = isset($param['status']) ? $param['status'] : null;
        $startDate = isset($param['startDate']) ? $param['startDate'] : null;
        $endDate = isset($param['endDate']) ? $param['endDate'].' 23:59:59' : null;
        $mobile = isset($param['mobile']) ? $param['mobile'] : null;
        $pageIndex = isset($param['currentPage']) ? $param['currentPage'] : 1;

        $orderRefundLogic = A('Common/OrderRefund', 'Logic');
        $orderRefundInfo = $orderRefundLogic->getInfo($hotelId, $status, $startDate, $endDate, $mobile, $pageIndex, C('PAGE_SIZE'));
        // 处理数据
        $returnItem = array();

        foreach ($orderRefundInfo['info'] as $orderRefundItem) {
            array_push($returnItem, array(
                'id' => &$orderRefundItem['id'],
                'orderNumber' => encodeOrderNumber($orderRefundItem['orderroom_id'], 'MZ'),
                'orderRoomId' => &$orderRefundItem['orderroom_id'],
                'startDate' => substr($orderRefundItem['orderRoom']['startdate'], 0, 10),
                'endDate' => substr($orderRefundItem['orderRoom']['checkoutdate'], 0, 10),
                'price' => &$orderRefundItem['price'],
                'refundPrice' => &$orderRefundItem['refundprice'],
                'reason' => &$orderRefundItem['reason'],
                'apply' => &$orderRefundItem['apply'],
                'status' => &$orderRefundItem['status'],
                'charityPrice' => &$orderRefundItem['order']['charityprice'],
                'user' => array(
                    'name' => &$orderRefundItem['order']['name'],
                    'areacode' => &$orderRefundItem['order']['areacode'],
                    'mobile' => &$orderRefundItem['order']['mobile'],
                    'identity' => &$orderRefundItem['order']['identity'],
                ),
                'roomTypeName' => &$orderRefundItem['orderRoom']['roomtype_name'],
                'roomName' => &$orderRefundItem['orderRoom']['roominfo_name'],
                'type' => &$orderRefundItem['type'],
            ));
            $orderRefundItem['user'] = array(
                'name' => $orderRefundItem['order']['name'],
                'areacode' => $orderRefundItem['order']['areacode'],
                'mobile' => $orderRefundItem['order']['mobile'],
                'identity' => $orderRefundItem['order']['identity'],
            );
        }
        unset($orderRefundItem);

        returnJson(200, 200, array(
            'item' => $returnItem,
            'page' => array(
                'currentPage' => isset($orderRefundInfo['page']['current']) ? $orderRefundInfo['page']['current'] : $pageIndex,
                'size' => isset($orderRefundInfo['page']['size']) ? $orderRefundInfo['page']['size'] : C('PAGE_SIZE'),
                'total' => isset($orderRefundInfo['page']['pageTotal']) ? $orderRefundInfo['page']['pageTotal'] : 0,
                'totalCount' => $orderRefundInfo['page']['total'],
            ),
        ));
    }

    /**
     * 获取客栈畅联代收信息
     * @modify trainyao 2016-05-17    创建
     * @modify trainyao 2016-06-20 添加押金信息
     */
    public function reconciliationItem() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'startDate' => 'notempty,isDate',
            'endDate' => 'notempty,isDate',
            'mobile' => 'notempty',
            'status' => 'notempty,booleanInt',
            'currentPage' => 'notempty,natureNumber',
        );

        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        // 校验客栈是否过期,是否禁用
        $hotelId = $param['hotel'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $result = $hotelLogic->checkHotelActive($hotelId);
        if ($result == false) {
            returnJson(403, 408);
            return;
        }
        $result = $hotelLogic->checkHotelStatus($hotelId);
        if ($result == false) {
            returnJson(403, 401);
            return;
        }

        // 校验是否有操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $hasRight = $adminAuthorityLogic->check($hotelId);
        if (!$hasRight) {
            returnJson(403, 202);
            return;
        }

        // 获取信息
        $status = isset($param['status']) ? $param['status'] : null;
        $startDate = isset($param['startDate']) ? $param['startDate'] : null;
        $endDate = isset($param['endDate']) ? $param['endDate'] . ' 23:59:59' : null;
        $mobile = isset($param['mobile']) ? $param['mobile'] : null;
        $pageIndex = isset($param['currentPage']) ? $param['currentPage'] : 1;

        $hotelPayLogic = A('Common/HotelPay', 'Logic');
        $reconciliationInfo = $hotelPayLogic->getReconciliationItem($hotelId, $status, $startDate, $endDate, $mobile, $pageIndex, C('PAGE_SIZE'), true);

        // 处理数据
        $returnItem = array();
        foreach ($reconciliationInfo['info'] as $reconciliationItem) {
            array_push($returnItem, array(
                'orderNumber' => &$reconciliationItem['outtradenumber'],
                'outOrderNumber' => &$reconciliationItem['tradenumber'],
                'payType' => &$reconciliationItem['paymethod'],
                'payTime' => &$reconciliationItem['paytime'],
                'price' => &$reconciliationItem['payfee'],
                'deposit' => &$reconciliationItem['deposit'],
                'createTime' => &$reconciliationItem['createtime'],
                'status' => &$reconciliationItem['ischeck'],
                'user' => array(
                    'name' => &$reconciliationItem['order']['name'],
                    'areacode' => &$reconciliationItem['order']['areacode'],
                    'mobile' => &$reconciliationItem['order']['mobile'],
                    'identity' => &$reconciliationItem['order']['identity'],
                ),
            ));
        }
        unset($reconciliationItem);

        returnJson(200, 200, array(
            'item' => $returnItem,
            'page' => array(
                'currentPage' => isset($reconciliationInfo['page']['current']) ? $reconciliationInfo['page']['current'] : $pageIndex,
                'totalCount' => $reconciliationInfo['page']['total'],
                'size' => isset($reconciliationInfo['page']['size']) ? $reconciliationInfo['page']['size'] : C('PAGE_SIZE'),
                'total' => isset($reconciliationInfo['page']['pageTotal']) ? $reconciliationInfo['page']['pageTotal'] : 0,
            ),
        ));
    }

    /**
     * 获取客栈商家自收条目
     * @modify trainyao 2016-07-08   创建
     */
    public function partnerPayItem() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'startDate' => 'notempty,isDate',
            'endDate' => 'notempty,isDate',
            'mobile' => 'notempty,mobile',
            'currentPage' => 'notempty,natureNumber',
        );

        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        // 校验客栈是否过期,是否禁用
        $hotelId = $param['hotel'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $result = $hotelLogic->checkHotelActive($hotelId);
        if ($result == false) {
            returnJson(403, 408);
            return;
        }
        $result = $hotelLogic->checkHotelStatus($hotelId);
        if ($result == false) {
            returnJson(403, 401);
            return;
        }

        // 校验是否有操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $hasRight = $adminAuthorityLogic->check($hotelId);
        if (!$hasRight) {
            returnJson(403, 202);
            return;
        }

        // 获取信息
        $startDate = isset($param['startDate']) ? $param['startDate'] : null;
        $endDate = isset($param['endDate']) ? $param['endDate'] . ' 23:59:59' : null;
        $mobile = isset($param['mobile']) ? $param['mobile'] : null;
        $pageIndex = isset($param['currentPage']) ? $param['currentPage'] : 1;

        $hotelPayLogic = A('Common/HotelPay', 'Logic');
        $reconciliationInfo = $hotelPayLogic->getReconciliationItem($hotelId, null, $startDate, $endDate, $mobile, $pageIndex, C('PAGE_SIZE'), false);

        // 处理数据
        $returnItem = array();
        foreach ($reconciliationInfo['info'] as $reconciliationItem) {
            array_push($returnItem, array(
                'orderNumber' => &$reconciliationItem['outtradenumber'],
                'outOrderNumber' => &$reconciliationItem['tradenumber'],
                'payType' => &$reconciliationItem['paymethod'],
                'payTime' => &$reconciliationItem['paytime'],
                'price' => &$reconciliationItem['payfee'],
                'deposit' => &$reconciliationItem['deposit'],
                'createTime' => &$reconciliationItem['createtime'],
                'user' => array(
                    'name' => &$reconciliationItem['order']['name'],
                    'mobile' => &$reconciliationItem['order']['mobile'],
                    'identity' => &$reconciliationItem['order']['identity'],
                ),
            ));
        }
        unset($reconciliationItem);

        returnJson(200, 200, array(
            'item' => $returnItem,
            'page' => array(
                'currentPage' => isset($reconciliationInfo['page']['current']) ? $reconciliationInfo['page']['current'] : $pageIndex,
                'size' => isset($reconciliationInfo['page']['size']) ? $reconciliationInfo['page']['size'] : C('PAGE_SIZE'),
                'total' => isset($reconciliationInfo['page']['pageTotal']) ? $reconciliationInfo['page']['pageTotal'] : 0,
            ),
        ));
    }

    /**
     * 获取客栈畅联代收/商家自收支付信息
     * @modify trainyao 2016-07-11    创建
     */
    public function payInfo() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        // 校验客栈是否过期,是否禁用
        $hotelId = $param['hotel'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $result = $hotelLogic->checkHotelActive($hotelId);
        if ($result == false) {
            returnJson(403, 408);
            return;
        }
        $result = $hotelLogic->checkHotelStatus($hotelId);
        if ($result == false) {
            returnJson(403, 401);
            return;
        }

        // 校验是否有操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $hasRight = $adminAuthorityLogic->check($hotelId);
        if (!$hasRight) {
            returnJson(403, 202);
            return;
        }

        // 获取支付类型
        $hotelPayLogic = A('Common/HotelPay', 'Logic');
        $returnData = $hotelPayLogic->getPartnerPayInfo($hotelId);
        // 支付功能开通情况
        $payUsable = array(
        	'uclbrtAlipayEnable'=>false,
        	'uclbrtWechatEnable'=>false,
        	'hotelAlipayEnable'=>false,
        	'hotelWechatEnable'=>false,
        );
        $hotelPayUsableModel = D('HotelPayUsable');
        $hotelPayUsableInfo = $hotelPayUsableModel->getInfo($hotelId);
        if(!empty($hotelPayUsableInfo)){
            $payUsable = array(
                    'uclbrtAlipayEnable'=>!empty($hotelPayUsableInfo['uclbrtalipayenable']) && time()<strtotime($hotelPayUsableInfo['uclbrtalipayenddate']),
                    'uclbrtWechatEnable'=>!empty($hotelPayUsableInfo['uclbrtwechatenable']) && time()<strtotime($hotelPayUsableInfo['uclbrtwechatenddate']),
                    'hotelAlipayEnable'=>!empty($hotelPayUsableInfo['hotelalipayenable']) && time()<strtotime($hotelPayUsableInfo['hotelalipayenddate']),
                    'hotelWechatEnable'=>!empty($hotelPayUsableInfo['hotelwechatenable']) && time()<strtotime($hotelPayUsableInfo['hotelwechatenddata']),
            );
        }
        $returnData['payUsable'] = $payUsable;

        returnJson(200, 200, $returnData);
    }

    /**
     * 保存客栈支付类型
     * @modify trainyao 2016-07-11    创建
     */
    public function togglePayType() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'type' => 'notnull,notempty',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }
        

        $type = $param['type'];
        if(!in_array($type, array('0','1','2','16','32','3','33','18','48','64','65','66','67','80','82','96','97','112'))){
            returnJson(403, 213);
            return;
        }

        // 校验客栈是否过期,是否禁用
        $hotelId = $param['hotel'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $result = $hotelLogic->checkHotelActive($hotelId);
        if ($result == false) {
            returnJson(403, 408);
            return;
        }
        $result = $hotelLogic->checkHotelStatus($hotelId);
        if ($result == false) {
            returnJson(403, 401);
            return;
        }

        // 校验是否有操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $hasRight = $adminAuthorityLogic->check($hotelId);
        if (!$hasRight) {
            returnJson(403, 202);
            return;
        }

        // 校验是否已经开通
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        // 校验是否是义卖客栈 只能畅联代收  可以开启会员卡支付
        $result = $hotelLogic->checkFreeHotel($hotelId);
        if ($result === true) {
            if(!in_array($type, array('0','1','2','3','64','65','66','67'))){
                returnJson(403, 422);
                return;
            }
        }

        // 保存支付类型
        $hotelPayLogic = A('Common/HotelPay', 'Logic');
        try {
            $hotelPayLogic->savePayType($hotelId, $type);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        returnJson(200, 200);
    }

    /**
     * 保存客栈订单最迟退房时间和最早入住时间
     * @modify wenhui 2017-08-29    创建
     */
    /*public function saveHotelOrderTime() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'checkoutTime' => 'notnull,notempty,isHourMinute',
            'checkinTime' => 'notnull,notempty,isHourMinute',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        if ($param['checkoutTime'] >= $param['checkinTime']) {
            returnJson(403, 419);
            return;
        }

        // 校验客栈是否过期,是否禁用
        $checkoutTime = $param['checkoutTime'];
        $checkinTime = $param['checkinTime'];
        $hotelId = $param['hotel'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $result = $hotelLogic->checkHotelActive($hotelId);
        if ($result == false) {
            returnJson(403, 408);
            return;
        }
        $result = $hotelLogic->checkHotelStatus($hotelId);
        if ($result == false) {
            returnJson(403, 401);
            return;
        }
        // 校验是否有操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $hasRight = $adminAuthorityLogic->check($hotelId);
        if (!$hasRight) {
            returnJson(403, 202);
            return;
        }

        // 保存
        $hotelPayLogic = A('Common/HotelPay', 'Logic');
        try {
            $hotelPayLogic->saveCheckoutOrCheckinTime($hotelId, $checkoutTime, $checkinTime);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        returnJson(200, 200);
    }*/

    /**
     * 保存客栈支付信息
     * @modify trainyao 2016-07-11    创建
     * @modify zhangzhen 2017-01-19 添加微信支付文件
     */
    public function savePayInfo() {
        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }
        $wechatPayInfo = null;
        $aliPayInfo = null;
        if (isset($param['wechat'])) {
            $wechatPayInfo = $param['wechat'];
            $validateRule = array(
                'appid' => 'notnull,notempty,length32',
                'appsecret' => 'notnull,notempty,length32',
                'mchid' => 'notnull,notempty,length32',
                'mchkey' => 'notnull,notempty,nameLength50',
                'mpverify' => 'notnull,notempty,mpverify,nameLength50',
            );
            $validatePass = $this->ajaxValidateReturn($this->validateUtil, $wechatPayInfo, $validateRule, array(
                'length32' => array('mblength', '0,32,utf-8', '403,213,error'),
                'mpverify' => array('regex', '/^MP_verify_[0-9A-Za-z]+(\.txt)?$/', '403,204,error'),
            ));
            if (empty($validatePass)) {
                return;
            }
        }
        if (isset($param['ali'])) {
            $aliPayInfo = $param['ali'];
            $validateRule = array(
                'account' => 'notnull,notempty,nameLength50',
                'pid' => 'notnull,notempty,length40',
                'key' => 'notnull,notempty,length40',
            );
            $validatePass = $this->ajaxValidateReturn($this->validateUtil, $aliPayInfo, $validateRule, array(
                'length40' => array('mblength', '0,40,utf-8', '403,213,error'),
            ));
            if (empty($validatePass)) {
                return;
            }
        }

        // 校验客栈是否过期,是否禁用
        $hotelId = $param['hotel'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $result = $hotelLogic->checkHotelActive($hotelId);
        if ($result == false) {
            returnJson(403, 408);
            return;
        }
        $result = $hotelLogic->checkHotelStatus($hotelId);
        if ($result == false) {
            returnJson(403, 401);
            return;
        }

        // 校验是否有操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $hasRight = $adminAuthorityLogic->check($hotelId);
        if (!$hasRight) {
            returnJson(403, 202);
            return;
        }

        // 保存支付类型
        $hotelPayLogic = A('Common/HotelPay', 'Logic');
        try {
            $hotelPayLogic->savePayInfo($hotelId, $wechatPayInfo, $aliPayInfo);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        returnJson(200, 200);
    }

    /**
     * 保存小黑包板内容
     * @modify wenhui 2017-07-28    创建
     */
    public function saveBlackboard() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'blackBoardContent' => 'notnull,nameLength500',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        // 校验客栈是否过期,是否禁用
        $hotelId = $param['hotel'];
        $blackBoardContent = isset($param['blackBoardContent']) ? $param['blackBoardContent'] : '';
        $hotelLogic = A('Common/Hotel', 'Logic');
        $result = $hotelLogic->checkHotelActive($hotelId);
        if ($result == false) {
            returnJson(403, 408);
            return;
        }
        $result = $hotelLogic->checkHotelStatus($hotelId);
        if ($result == false) {
            returnJson(403, 401);
            return;
        }

        // 校验是否已经开通
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        $result = $hotelLogic->saveHotelBlackboard($hotelId, $blackBoardContent);

        returnJson(200, 200, array('hotel' => $hotelId, 'blackBoardContent' => $blackBoardContent));
    }

    /**
     * 获取小黑包板内容
     * @modify wenhui 2017-07-28    创建
     */
    public function getBlackboard() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        // 校验客栈是否过期,是否禁用
        $hotelId = $param['hotel'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $result = $hotelLogic->checkHotelActive($hotelId);
        if ($result == false) {
            returnJson(403, 408);
            return;
        }
        $result = $hotelLogic->checkHotelStatus($hotelId);
        if ($result == false) {
            returnJson(403, 401);
            return;
        }

        // 校验是否已经开通
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        $blackData = $hotelLogic->getHotelBlackboard($hotelId);

        returnJson(200, 200, $blackData);
    }

    /**
     * 获提醒内容
     * @modify wenhui 2017-07-28    创建
     */
    public function getRemindList() {
        $param = I('post.');

        $validateRule = array(
            'currentPage' => 'notnull,notempty',
            'pageSize' => 'notnull,notempty',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        $userId = getUserId();
        if (isset($param['hotel'])) {
            $hotelId = $param['hotel'];
        } else {
            $AdminHotelModel = D('AdminHotel');
            $adminHotelInfo = $AdminHotelModel->getInfoByUserLeftHotelEntity($userId);
            $hotelId = array_column($adminHotelInfo, 'hotelentity_id');
        }

        $currentPage = isset($param['currentPage']) ? $param['currentPage'] : 1;
        $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 15;
        $keys = isset($param['keys']) && !empty($param['keys']) ? $param['keys'] : null;
        $reminded = isset($param['remind']) ? $param['remind'] : null;
        $newstype = isset($param['isRead']) ? $param['isRead'] : null;

        $hotelLogic = A('Common/Hotel', 'Logic');
        $remindList = $hotelLogic->getRemindList($hotelId, $currentPage, $pageSize, $keys, $reminded, $newstype);
        if (empty($remindList) || empty($remindList['remindList'])) {
            returnJson(200, 200, array());
        }

        returnJson(200, 200, $remindList);
    }

    /**
     * 保存闹钟提醒数据
     * @modify wenhui 2017-07-28    创建
     */
    public function saveRemind() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'remindDate' => 'notnull,notempty',
            'content' => 'notnull,notempty,nameLength60',
        );
        // 删除 不检查 remindDate content
        if (isset($param['dropRemind']) || isset($param['isRead'])) {
            unset($validateRule['remindDate']);
            unset($validateRule['content']);
        }

        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        $hotelId = $param['hotel'];
        $remindDate = isset($param['remindDate']) ? $param['remindDate'] : '';
        $content = $param['content'];
        $isRead = isset($param['isRead']) ? $param['isRead'] : null;
        $alarmClockId = isset($param['alarmClockId']) && !empty($param['alarmClockId']) ? $param['alarmClockId'] : null;
        $dropRemind = isset($param['dropRemind']) ? $param['dropRemind'] : null;

        // 校验客栈是否过期,是否禁用
        $hotelLogic = A('Common/Hotel', 'Logic');
        $result = $hotelLogic->checkHotelActive($hotelId);
        if ($result == false) {
            returnJson(403, 408);
            return;
        }
        $result = $hotelLogic->checkHotelStatus($hotelId);
        if ($result == false) {
            returnJson(403, 401);
            return;
        }

        $saveResult = $hotelLogic->saveRemind($hotelId, $remindDate, $content, $alarmClockId, $dropRemind, $isRead);

        returnJson(200, 200);
    }

    /**
     * 获取小黑包板内容
     * @modify wenhui 2017-07-28    创建
     */
    public function getRemind() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'alarmClockId' => 'notnull,notempty,id',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        // 校验客栈是否过期,是否禁用
        $hotelId = $param['hotel'];
        $alarmClockId = isset($param['alarmClockId']) && !empty($param['alarmClockId']) ? $param['alarmClockId'] : null;
        $hotelLogic = A('Common/Hotel', 'Logic');
        $result = $hotelLogic->checkHotelActive($hotelId);
        if ($result == false) {
            returnJson(403, 408);
            return;
        }
        $result = $hotelLogic->checkHotelStatus($hotelId);
        if ($result == false) {
            returnJson(403, 401);
            return;
        }

        // 校验是否已经开通
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        $remindData = $hotelLogic->getRemind($alarmClockId);

        returnJson(200, 200, array(
            'alarmClockId' => $remindData['id'],
            'hotel' => $remindData['hotelentity_id'],
            'content' => $remindData['content'],
            'remindDate' => date('Y-m-d H:i', strtotime($remindData['remindtime'])),
        ));
    }

    /**
     * 保存客栈办理入住状态
     * @modify wenhui 2017-10-10    创建
     */
    public function toggleCheckinType() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'type' => 'notnull,notempty',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }
        

        $type = $param['type'];
        if(!in_array($type, array('0','1','2','3'))){
            returnJson(403, 213);
            return;
        }

        // 校验客栈是否过期,是否禁用
        $hotelId = $param['hotel'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $result = $hotelLogic->checkHotelActive($hotelId);
        if ($result == false) {
            returnJson(403, 408);
            return;
        }
        $result = $hotelLogic->checkHotelStatus($hotelId);
        if ($result == false) {
            returnJson(403, 401);
            return;
        }

        // 校验是否有操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $hasRight = $adminAuthorityLogic->check($hotelId);
        if (!$hasRight) {
            returnJson(403, 202);
            return;
        }

        // 校验是否已经开通
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        // 保存客栈办理入住状态
        try {
            $hotelLogic->saveCheckinType($hotelId, $type);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        returnJson(200, 200);
    }

    /**
     * 获取客栈办理入住状态
     * @modify wenhui 2017-10-10    创建
     */
    public function toggleCheckinInfo() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        // 校验客栈是否过期,是否禁用
        $hotelId = $param['hotel'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $result = $hotelLogic->checkHotelActive($hotelId);
        if ($result == false) {
            returnJson(403, 408);
            return;
        }
        $result = $hotelLogic->checkHotelStatus($hotelId);
        if ($result == false) {
            returnJson(403, 401);
            return;
        }

        // 校验是否有操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $hasRight = $adminAuthorityLogic->check($hotelId);
        if (!$hasRight) {
            returnJson(403, 202);
            return;
        }

        $hotelCheckinUsableModel = D('HotelCheckinUsable');
        $checkinUsableInfo = $hotelCheckinUsableModel->getInfo($hotelId);
        if (empty($checkinUsableInfo)) {
            $hotelIdCheckinEnable = false;
            $hotelCheckinEnable = false;
        } else {
            $hotelIdCheckinEnable = in_array($checkinUsableInfo['type'], [1,3]) ? true : false;
            $hotelCheckinEnable = in_array($checkinUsableInfo['type'], [2,3]) ? true : false;
        }

        $returnData['checkinUsable'] = array(
            'hotelIdCheckinEnable' => $hotelIdCheckinEnable,
            'hotelCheckinEnable' => $hotelCheckinEnable,
        );

        returnJson(200, 200, $returnData);
    }

    /**
     * 添加记一笔项目
     * @modify wenhui 2017-12-19 创建
     */
    public function addNote() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'name' => 'notnull,notempty,nameLength15',
            'type' => 'notnull,notempty,id',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        $type = $param['type']; // 1=收入项目 2=支出项目
        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $noteName = $param['name'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $noteId = $hotelLogic->addNote($hotelId, $noteName, $type);

        returnJson(200, 200, ['noteId'=>$noteId, 'noteName'=>$noteName]);
    }

    /**
     * 修改记一笔项目
     * @modify wenhui 2017-12-19 创建
     */
    public function editNote() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'noteId' => 'notnull,notempty,id',
            'name' => 'notnull,notempty,nameLength15',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        $noteId = $param['noteId'];
        $name = $param['name'];
        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $hotelLogic = A('Common/Hotel', 'Logic');
        $noteId = $hotelLogic->editNote($hotelId, $noteId, $name);

        returnJson(200, 200);
    }

    /**
     * 删除记一笔项目
     * @modify wenhui 2017-12-19 创建
     */
    public function deleteNote() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'noteId' => 'notnull,notempty,id',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $hotelNoteId = $param['noteId'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $addResult = $hotelLogic->deleteNote($hotelId, $hotelNoteId);

        returnJson(200, 200);
    }

    /**
     * 添加他消费项目
     * @modify wenhui 2017-12-19 创建
     */
    public function addOtherCost() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'name' => 'notnull,notempty,nameLength15',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $name = $param['name'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $addResult = $hotelLogic->addOtherCost($hotelId, $name);

        returnJson(200, 200);
    }

    /**
     * 修改他消费项目
     * @modify wenhui 2017-12-19 创建
     */
    public function editOtherCost() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'costId' => 'notnull,notempty,id',
            'name' => 'notnull,notempty,nameLength15',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $costId = $param['costId'];
        $name = $param['name'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $addResult = $hotelLogic->editOtherCost($hotelId, $costId, $name);

        returnJson(200, 200);
    }

    /**
     * 删除他消费项目
     * @modify wenhui 2017-12-19 创建
     */
    public function deleteOtherCost() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'costId' => 'notnull,notempty,id',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $costId = $param['costId'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $addResult = $hotelLogic->deleteOtherCost($hotelId, $costId);

        returnJson(200, 200);
    }

    /**
     * 添加查房消费物品
     * @modify wenhui 2017-12-19 创建
     */
    public function addRoomCost() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'name' => 'notnull,notempty,nameLength15',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $name = $param['name'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $addResult = $hotelLogic->setRoomCost($hotelId, $name);

        returnJson(200, 200);
    }

    /**
     * 修改查房消费物品
     * @modify wenhui 2017-12-19 创建
     */
    public function editRoomCost() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'costId' => 'notnull,notempty,id',
            'name' => 'notnull,notempty,nameLength15',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $costId = $param['costId'];
        $name = $param['name'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $saveResult = $hotelLogic->saveRoomCost($hotelId, $costId, $name);

        returnJson(200, 200);
    }

    /**
     * 删除查房消费物品
     * @modify wenhui 2017-12-19 创建
     */
    public function deleteRoomCost() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'costId' => 'notnull,notempty,id',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $costId = $param['costId'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $saveResult = $hotelLogic->deleteRoomCost($hotelId, $costId);

        returnJson(200, 200);
    }

    /**
     * 保存客栈查房开关
     * @modify wenhui 2018-02-09    创建
     */
    public function toggleRoomCheck() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'type' => 'notnull,notempty,booleanInt',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }
        
        $type = $param['type'];
        // 校验客栈是否过期,是否禁用
        $hotelId = $param['hotel'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $result = $hotelLogic->checkHotelActive($hotelId);
        if ($result == false) {
            returnJson(403, 408);
            return;
        }
        $result = $hotelLogic->checkHotelStatus($hotelId);
        if ($result == false) {
            returnJson(403, 401);
            return;
        }

        // 校验是否有操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $hasRight = $adminAuthorityLogic->check($hotelId);
        if (!$hasRight) {
            returnJson(403, 202);
            return;
        }

        // 保存客栈查房开关
        try {
            $hotelLogic->saveRoomCheck($hotelId, $type);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        returnJson(200, 200);
    }

    /**
     * 获取客栈查房开关
     * @modify wenhui 2018-02-09    创建
     */
    public function getToggleCheck() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }
        
        // 校验客栈是否过期,是否禁用
        $hotelId = $param['hotel'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $result = $hotelLogic->checkHotelActive($hotelId);
        if ($result == false) {
            returnJson(403, 408);
            return;
        }
        $result = $hotelLogic->checkHotelStatus($hotelId);
        if ($result == false) {
            returnJson(403, 401);
            return;
        }

        // 校验是否有操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $hasRight = $adminAuthorityLogic->check($hotelId);
        if (!$hasRight) {
            returnJson(403, 202);
            return;
        }

        // 获取客栈查房开关
        try {
            $returnData = $hotelLogic->getRoomCheck($hotelId);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        returnJson(200, 200, $returnData);
    }


     /**
     * 获取记一笔记录
     * @modify wenhui 2017-12-19 创建
     */
    public function getNoteCost() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'noteCostId' => 'notnull,notempty,id',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        $noteCostId = $param['noteCostId'];
        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $noteName = $param['name'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $data = $hotelLogic->getNoteCost($hotelId, $noteCostId);
        $data = !empty($data) ? $data[0] : [];

        returnJson(200, 200, $data);
    }

    /**
     * 添加记一笔记录
     * @modify wenhui 2017-12-19 创建
     */
    public function addNoteCost() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'noteId' => 'notnull,notempty,id',
            'payId' => 'notnull,notempty,id',
            'type' => 'notnull,notempty,id',
            'price' => 'notnull,notempty,decimal8_2',
            'noteDate' => 'notnull,notempty,isDate',
            'remark' => 'nameLength100',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        $noteId = $param['noteId'];
        $payId = $param['payId'];
        $type = $param['type']; // 1=收入 2=支出
        $price = $param['price'];
        $noteDate = $param['noteDate'];
        $remark = $param['remark'];
        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $noteName = $param['name'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $addResult = $hotelLogic->addNoteCost($hotelId, $noteId, $payId, $type, $price, $noteDate, $remark);

        returnJson(200, 200);
    }

    /**
     * 编辑记一笔记录
     * @modify wenhui 2017-12-19 创建
     */
    public function editNoteCost() {
        $param = I('post.');

        $isDelete = isset($param['delete']) && $param['delete'] == 'delete' ? true : null;
        if (is_null($isDelete)) {
            $validateRule = array(
                'hotel' => 'notnull,notempty,id',
                'noteCostId' => 'notnull,notempty,id',
                'noteId' => 'notnull,notempty,id',
                'payId' => 'notnull,notempty,id',
                'type' => 'notnull,notempty,id',
                'price' => 'notnull,notempty,decimal8_2',
                'noteDate' => 'notnull,notempty,isDate',
                'remark' => 'nameLength100',
            );
        } else {
            $validateRule = array(
                'hotel' => 'notnull,notempty,id',
                'noteCostId' => 'notnull,notempty,id',
            );
        }

        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        $noteCostId = $param['noteCostId'];
        $noteId = isset($param['noteId']) ? $param['noteId'] : null;
        $payId = isset($param['payId']) ? $param['payId'] :mull;
        $type = isset($param['type']) ? $param['type'] : null; // 1=收入 2=支出
        $price = isset($param['price']) ? $param['price'] : null;
        $noteDate = isset($param['noteDate']) ? $param['noteDate'] : null;
        $remark = isset($param['remark']) ? $param['remark'] : null;

        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $noteName = $param['name'];
        $hotelLogic = A('Common/Hotel', 'Logic');
        $addResult = $hotelLogic->editNoteCost($hotelId, $noteCostId, $noteId, $payId, $type, $price, $noteDate, $remark, $isDelete);

        // 返回数据
        $data = $hotelLogic->getNoteCost($hotelId, $noteCostId);
        $data = !empty($data) ? $data[0] : [];

        returnJson(200, 200, $data);
    }

    /**
     * 保存客栈默认货币 或 是否提示设置默认货币 即 $currencyId = null
     * @modify wenhui 2017-12-19 创建
     */
    public function saveHotelDefaultCurrency() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
        );
        if (isset($param['currencyId'])) {
            $validateRule['currencyId'] = 'notnull,notempty,id';
        }

        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        $currencyId = isset($param['currencyId']) ? $param['currencyId'] : null;

        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $currencyLogic = A('Common/Currency', 'Logic');
        try {
            $isSetDefaultCurrency = $currencyLogic->addUserHotelDefaultCurrency($hotelId, $currencyId, 1);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        $data['isSetDefaultCurrency'] = $isSetDefaultCurrency;
        returnJson(200, 200, $data);
    }

    /**
     * 删除客栈货币
     * @modify wenhui 2017-12-19 创建
     */
    public function deleteHotelCurrency() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'hotelCurrencyId' => 'notnull,notempty,id',
        );

        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        $hotelCurrencyId = $param['hotelCurrencyId'];

        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $currencyLogic = A('Common/Currency', 'Logic');
        try {
            $saveResult = $currencyLogic->saveHotelCurrency($hotelCurrencyId, null, IS_DELETE);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        returnJson(200, 200);
    }

    /**
     * 添加客栈货币
     * @modify wenhui 2017-12-19 创建
     */
    public function setHotelCurrency() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'currencyId' => 'notnull,notempty,id',
        );

        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        $currencyId = $param['currencyId'];

        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $currencyLogic = A('Common/Currency', 'Logic');
        try {
            $addResult = $currencyLogic->addHotelCurrency($hotelId, $currencyId, 0);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        returnJson(200, 200);
    }

    /**
     * 添加客栈货币
     * @modify wenhui 2017-12-19 创建
     */
    public function getHotelCurrency() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
        );

        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];

        // 检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId);

        $currencyLogic = A('Common/Currency', 'Logic');
        $currencyHotel = $currencyLogic->takeHotelCurrency($hotelId);

        returnJson(200, 200, $currencyHotel);
    }

    /**
     * 获取转换汇率
     * @modify wenhui 2017-12-19 创建
     */
    public function getTransCrrencyRate() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'transData' => 'notnull,notempty',
        );
        $validateRuleTrans = array(
            'transCurrencyId' => 'notnull,notempty,id',
            'value' => 'notnull,notempty,decimal8_2',
            'type' => 'notnull,notempty,id',
            'isDeposit' => 'notnull,notempty,id',
        );

        if (!isset($param['transData']) || empty($param['transData'])) {
            returnJson(200, 200);
            return;
        }

        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        foreach ($param['transData'] as $key => $trans) {
            $this->ajaxValidateReturn($this->validateUtil, $trans, $validateRuleTrans);
        }
        unset($trans);

        $hotelId = $param['hotel'];
        $transData = $param['transData'];

        // 获取默认货币
        $currencyHotelModel = D('CurrencyHotel');
        $defaultCurrency = $currencyHotelModel->getDefaultCurrency($hotelId);
        $currencyId = isset($defaultCurrency['currency_id']) ? $defaultCurrency['currency_id'] : 8 ; // 人民币

        $currencyLogic = A('Common/Currency', 'Logic');
        foreach ($transData as $key => &$trans) {
            if ($currencyId != $trans['transCurrencyId'] && $trans['value'] != 0) {
                $trans['value'] = $currencyLogic->exchangeRates($trans['transCurrencyId'], $currencyId, $trans['value']);
            }
        }

        returnJson(200, 200, ['transData' => $transData]);
    }
}
